package com.nuotu.datawall.clouddata;

import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.text.InputType;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.nuotu.datawall.clouddata.adapter.SettingActivityLVAdapter;
import com.nuotu.datawall.clouddata.base.BaseActivity;
import com.nuotu.datawall.clouddata.util.FinalStr;
import com.nuotu.datawall.clouddata.util.PrefUtils;
import com.nuotu.datawall.clouddata.util.ServerConfig;
import com.nuotu.datawall.clouddata.util.ToastUtils;
import com.nuotu.datawall.clouddata.util.Tool;
import com.nuotu.datawall.clouddata.view.MyDialog;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;

@ContentView(R.layout.act_setting)
public class SettingActivity extends BaseActivity {
    public static final String[] SETTING_WORD_KEY = {"IP", "图书馆ID","图书馆密匙","账号","密码","版本号", "退出系统"};

    private final  int CONFIG_IP=0;//IP
    private final  int CONFIG_LIBID=1;//图书馆ID
    private final  int CONFIG_LIBSECRET=2;//图书馆密匙
    private final  int CONFIG_USERNAME=3;//账号
    private final  int CONFIG_PASSWORD=4;//密码
    private final  int CONFIG_BANBENHAO=5;//版本号
    private final  int CONFIG_CLOSESYSTEM=6;//退出系统

    private ArrayList<HashMap<String, Object>> list;
    private SettingActivityLVAdapter adapter;
    @ViewInject(R.id.act_setting_lv)
    ListView listView;
    @ViewInject(R.id.act_setting_iv_back)
    ImageView act_setting_iv_back;
    private MyDialog setDatedialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         initView();
    }


    private void initView() {
        fillData();
        adapter = new SettingActivityLVAdapter(this, list);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case CONFIG_IP://IP
                    case CONFIG_LIBID://
                    case CONFIG_LIBSECRET://
                    case CONFIG_USERNAME://
                    case CONFIG_PASSWORD://
                      showSettingPwdDialog(position);
                        break;
                    case CONFIG_CLOSESYSTEM://退出系统
                        break;
                }
            }
        });
        act_setting_iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ServerConfig.initUrl(mContext);//初始化各请求连接
                finish();
                Tool.restartApplication(mContext);
            }
        });
    }

    private void showSettingPwdDialog(final int index) {
        View view  = getLayoutInflater().inflate(R.layout.dialog_setting_edit_b, null);
        final TextView title = (TextView) view.findViewById(R.id.tv_edit_title);
        final EditText et_data = (EditText) view.findViewById(R.id.tv_edit_content);
        switch (index) {
            case CONFIG_IP:
                title.setText("设置IP");
                et_data.setHint("例：127.0.0.1");
//                et_data.setInputType(InputType.TYPE_CLASS_PHONE);
                et_data.setText(PrefUtils.getString(this, FinalStr.SERVER_IP_KEY, ""));
                break;
            case CONFIG_LIBID:
                title.setText("设置"+SETTING_WORD_KEY[CONFIG_LIBID]);
                et_data.setText(PrefUtils.getString(this, FinalStr.SERVER_LIBID_KEY, ""));
                break;
            case CONFIG_LIBSECRET:
                title.setText("设置"+SETTING_WORD_KEY[CONFIG_LIBSECRET]);
                et_data.setText(PrefUtils.getString(this, FinalStr.SERVER_LIBSECRET_KEY, ""));
                break;
            case CONFIG_USERNAME:
                title.setText("设置"+SETTING_WORD_KEY[CONFIG_USERNAME]);
                et_data.setText(PrefUtils.getString(this, FinalStr.SERVER_USERNAME_KEY, ""));
                break;
            case CONFIG_PASSWORD:
                title.setText("设置"+SETTING_WORD_KEY[CONFIG_PASSWORD]);
                et_data.setText(PrefUtils.getString(this, FinalStr.SERVER_PASSWORD_KEY, ""));
                break;
            default:
                break;
        }
        Button btnOk = (Button) view.findViewById(R.id.btn_ok);
        Button btnCancel = (Button) view.findViewById(R.id.btn_cancel);

        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 判断二次输入的密码是否一至，如果一样就保存起来
                String str = et_data.getText().toString();
                if (TextUtils.isEmpty(str)) {
                    ToastUtils.toastShort("请输入数据");
                    return;
                }
                str=str.replaceAll("\n","").replaceAll("\r","").replaceAll(" ","");
                switch (index) {
                    case CONFIG_IP:
                        PrefUtils.setString(mContext, FinalStr.SERVER_IP_KEY, str);
                        ToastUtils.toastShort("IP设置成功");
                        break;
                    case CONFIG_LIBID:
                        PrefUtils.setString(mContext, FinalStr.SERVER_LIBID_KEY, str);
                        ToastUtils.toastShort(SETTING_WORD_KEY[CONFIG_LIBID]+"设置成功");
                        break;
                    case CONFIG_LIBSECRET:
                        PrefUtils.setString(mContext, FinalStr.SERVER_LIBSECRET_KEY, str);
                        ToastUtils.toastShort(SETTING_WORD_KEY[CONFIG_LIBSECRET]+"设置成功");
                        break;
                    case CONFIG_USERNAME:
                        PrefUtils.setString(mContext, FinalStr.SERVER_USERNAME_KEY, str);
                        ToastUtils.toastShort(SETTING_WORD_KEY[CONFIG_USERNAME]+"设置成功");
                        break;
                    case CONFIG_PASSWORD:
                        PrefUtils.setString(mContext, FinalStr.SERVER_PASSWORD_KEY, str);
                        ToastUtils.toastShort(SETTING_WORD_KEY[CONFIG_PASSWORD]+"设置成功");
                        break;
                }
                onResume();
                //关闭键盘
                Tool.overInputkeyboard(mContext, et_data);
                // 关闭对话框
                setDatedialog.dismiss();
            }
        });
        // 取消按钮的点击事件
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //关闭键盘
                Tool.overInputkeyboard(mContext, et_data);
                setDatedialog.dismiss();
            }
        });

        setDatedialog = new MyDialog(mContext);
        setDatedialog.setOnBeforeDismissListener(new MyDialog.OnBeforeDismissListener() {
            @Override
            public void onDismiss() {
                Tool.overInputkeyboard(mContext, et_data);
            }
        });
        setDatedialog.setView(view, 0, 0, 0, 0); // 设置view 和左上右下的间距

        setDatedialog.getWindow().setGravity(Gravity.CENTER_HORIZONTAL);
        setDatedialog.show();
        //设置窗口的大小
        setDatedialog.getWindow().setLayout(600, 350);
    }

    @Override
    protected void onResume() {
        super.onResume();
        initView();
    }
    private void fillData() {
        String[] wordArr = SETTING_WORD_KEY;
        int wordLen = wordArr.length;
        list = new ArrayList<>();
        for (int i = 0; i < wordLen; i++) {
            HashMap<String, Object> map = new HashMap<>();
            map.put(FinalStr.SETTING_ACT_WORD_KEY, wordArr[i]);
            switch (i) {
                case CONFIG_IP:
                    map.put(FinalStr.SETTING_ACT_TYPE_KEY, FinalStr.SETTING_ACT_TYPE_TV);
                    map.put(FinalStr.SETTING_ACT_DESC_KEY, PrefUtils.getString(this, FinalStr.SERVER_IP_KEY, "未配置"));
                    break;
                case CONFIG_LIBID:
                    map.put(FinalStr.SETTING_ACT_TYPE_KEY, FinalStr.SETTING_ACT_TYPE_TV);
                    map.put(FinalStr.SETTING_ACT_DESC_KEY, PrefUtils.getString(this, FinalStr.SERVER_LIBID_KEY, "未配置"));
                    break;
                case CONFIG_LIBSECRET:
                    map.put(FinalStr.SETTING_ACT_TYPE_KEY, FinalStr.SETTING_ACT_TYPE_TV);
                    map.put(FinalStr.SETTING_ACT_DESC_KEY, PrefUtils.getString(this, FinalStr.SERVER_LIBSECRET_KEY, "未配置"));
                    break;
                case CONFIG_USERNAME:
                    map.put(FinalStr.SETTING_ACT_TYPE_KEY, FinalStr.SETTING_ACT_TYPE_TV);
                    map.put(FinalStr.SETTING_ACT_DESC_KEY, PrefUtils.getString(this, FinalStr.SERVER_USERNAME_KEY, "未配置"));
                    break;
                case CONFIG_PASSWORD:
                    map.put(FinalStr.SETTING_ACT_TYPE_KEY, FinalStr.SETTING_ACT_TYPE_TV);
                    map.put(FinalStr.SETTING_ACT_DESC_KEY, PrefUtils.getString(this, FinalStr.SERVER_PASSWORD_KEY, "未配置"));
                    break;
                case CONFIG_BANBENHAO:
                    map.put(FinalStr.SETTING_ACT_TYPE_KEY, FinalStr.SETTING_ACT_TYPE_TV);
                    map.put(FinalStr.SETTING_ACT_DESC_KEY, "诺图大数据展示墙V_"+ Tool.getAppVersionName(mContext));
                    break;
                default:
                    map.put(FinalStr.SETTING_ACT_TYPE_KEY, "0");
                    map.put(FinalStr.SETTING_ACT_DESC_IMG_KEY, R.drawable.arrow_right);
                    break;
            }
            list.add(map);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
